import { any, must } from "../core/logic";
import { Engine } from "../Engine";
import { guid } from "../core/utils";
import { particle } from "./shader/Computer";
/**
 * @class
 * @memberof w.compute
 */
class ParticleComputer {
  constructor(opts = {}) {
    this.emitterParamsBuffer = must(opts.emitterParamsBuffer);
    this.frameParamsBuffer = must(opts.frameParamsBuffer);
    this.sizesBuffer = must(opts.sizesBuffer);
    this.sizeTimesBuffer = must(opts.sizeTimesBuffer);
    this.colorsBuffer = must(opts.colorsBuffer);
    this.colorTimesBuffer = must(opts.colorTimesBuffer);
    this.opacitysBuffer = must(opts.opacitysBuffer);
    this.opacityTimesBuffer = must(opts.opacityTimesBuffer);
    this.particleBuffer = must(opts.particleBuffer);
    this.particleNum = must(opts.particleNum);
    this.id=must(opts.id);
    this.initPipeLine();
    this.initBindGroup()
  }

  initPipeLine() {
    this.workgroups= Math.ceil(this.particleNum / 64)
    this.commandEncoder = Engine.instance.device.createCommandEncoder();
    this.computePipeline = Engine.instance.device.createComputePipeline({
      layout: `auto`,
      compute: {
        module: Engine.instance.device.createShaderModule({
          code: particle,
        }),
        entryPoint: "main",
      },
    });

  }
  initBindGroup() {
    if (!this.bindGroup) {
      this.bindGroup = Engine.instance.device.createBindGroup({
        layout: this.computePipeline.getBindGroupLayout(0),
        entries: [
          {
            binding: 0,
            resource: {
              buffer: this.emitterParamsBuffer,
            },
          },
          {
            binding: 1,
            resource: {
              buffer: this.frameParamsBuffer,
            },
          },
          {
            binding: 2,
            resource: {
              buffer: this.sizesBuffer,
            },
          },
          {
            binding: 3,
            resource: {
              buffer: this.sizeTimesBuffer,
            },
          },
          {
            binding: 4,
            resource: {
              buffer: this.colorsBuffer,
            },
          },
          {
            binding: 5,
            resource: {
              buffer: this.colorTimesBuffer,
            },
          },
          {
            binding: 6,
            resource: {
              buffer: this.opacitysBuffer,
            },
          },
          {
            binding: 7,
            resource: {
              buffer: this.opacityTimesBuffer,
            },
          },
          {
            binding: 8,
            resource: {
              buffer: this.particleBuffer,
              // offset: 0,
              // size: this.particleBuffer.size,
            },
          },
        ],
      });
    }
    return this.bindGroup;
  }
  onCompute(commandEncoder) {
    const computePass = commandEncoder.beginComputePass();
    computePass.setPipeline(this.computePipeline);
    computePass.setBindGroup(0, this.bindGroup);
    computePass.dispatchWorkgroups(this.workgroups);
    computePass.end();
  }
}
export { ParticleComputer };
